<template>
	<view class="page-body">
		<vk-data-table-query
			v-model="queryForm.formData"
			:columns="queryForm.columns"
			@search="search"
		></vk-data-table-query>
		<view>
			<el-row>
				<el-button type="success" size="small" icon="el-icon-circle-plus-outline" @click="addBtn">添加</el-button>
			</el-row>
		</view>
		<!-- :right-btns-more="table.rightBtnsMore" -->
		<vk-data-table
			ref="table"
			:action="table.action"
			:columns="table.columns"
			:query-form-param="queryForm"
			:right-btns="['update','delete']"
			:default-expand-all="true"
			@update="updateBtn"
			@delete="deleteBtn"
			@current-change="currentChange"
		></vk-data-table>
		<vk-data-dialog
			v-model="form.props.show"
			:title="form.props.title"
			width="500px"
			mode="form"
			:close-on-click-modal="false"
		>
			<vk-data-form
				v-model="form.data"
				:rules="form.props.rules"
				:action="form.props.action"
				:form-type="form.props.formType"
				:columns='form.props.columns'
				label-width="100px"
				@success="form.props.show = false;refresh();"
			></vk-data-form>
		</vk-data-dialog>
	</view>
</template>


<script>
	var that;								// 当前页面对象
	var vk = uni.vk;									// vk实例

	var originalForms = {};						// 表单初始化数据
	
	export default {
		data() {
			// 页面数据变量
			return {
				// 页面是否请求中或加载中
				loading:false,
				// init请求返回的数据
				data:{
				
				},
				queryForm:{
					// 查询表单数据源，可在此设置默认值
					formData:{
				
					},
					// 查询表单的字段规则 fieldName:指定数据库字段名,不填默认等于key
					columns:[
						{ key:"name", title:"名称", type:"text", width:160, mode:"%%" },
						{ key:"_add_time", title:"添加时间", type:"datetimerange", width:400, mode:"[]" },
					]
				},
				form:{
					// 表单请求数据，此处可以设置默认值
					data: {
						show: true,
						can_edit: true,
						view_permissions: 0
					},
					// 表单属性
					props: {
						// 表单请求地址
						action:"",
						// 表单字段显示规则
						columns:[
							{ key:"_id", title:"分类id", type:"text",disabled:true},
							{ key:"category_id", title:"分类标识", type:"text", tips:"设置一个能表达分类唯一含义的标识"},
							{ key:"name", title:"分类名称", type:"text", placeholder:"请输入分类名称" },
							{ key:"parent_id", title:"父级标识", type:"text", tips:"父级的category_id" },
							{
								key:"icon", title:"分类图标", type:"image",
								limit:1
							},
							{ key:"sort", title:"分类顺序", type:"number", placeholder:"请输入分类顺序"},
							{ key:"show", title:"是否显示", type:"switch",
								tips:"当设为false时，分类不显示。"
							},
							{ key:"can_edit", title:"是否可编辑", type:"switch",
								tips:"系统默认分类不能删除"
							},
							{ 
								key:"view_permissions", 
								title:"权限", 
								type:"radio" ,
								optionType:'button',
								data:[
									{ value:0, label:"默认" },
									{ value:1, label:"登录" },
									{ value:2, label:"登录及本地" }
								]
							},
							{ key:"description", title:"分类描述", type:"textarea",maxlength:"99",showWordLimit:true,autosize:{ minRows: 2, maxRows: 10 },
								tips:"设置一个备注来更详细的描述此分类的含义"
							},
						],
						// 表单验证规则
						rules:{
							category_id:[
								{ required:true, message:'菜单标识不能为空', trigger:'blur' },
								{ min: 2, max: 24, message:'长度在 2 到 24 个字符', trigger:'blur' },
							],
							name:[
								{ required:true, message: "请输入分类名称", trigger:"change" }
							],
							sort:[
								{ required:true, message: "请输入顺序", trigger:"change" },
								{ type:"number", message:"顺序必须是数字", trigger:"blur" },
							],
							icon:[
								{ required:true, message:"图标不能为空"},
							],
						},
						// add 代表添加 update 代表修改
						formType:"",
						// 是否显示表单的弹窗
						show:false
					}
				},
				table:{
					// 表格数据请求地址
					action:"admin/category/sys/getList",
					// 表格字段显示规则
					columns:[
						{ key:"category_id", title:"分类唯一标识", type:"text", width:200, align:"center" },
						{ key:"_id", title:"分类id", type:"text", width:200, align:"center" },
						{ key:"name", title:"类别名称", type:"text"},
						{ key:"icon", title:"类别图标", type:"image" },
						{ key:"show", title:"是否显示", type:"tag", defaultValue:true,
							data:[
								{ value:true, label:"显示", tagType:"success" },
								{ value:false, label:"隐藏", tagType:"danger" },
							]
						},
						{ key:"can_edit", title:"是否可编辑", type:"tag", defaultValue:true,
							data:[
								{ value:true, label:"可编辑", tagType:"success" },
								{ value:false, label:"不可编辑", tagType:"danger" },
							]
						},
						{ key:"view_permissions", title:"查看权限", type:"tag", defaultValue:true,
							data:[
								{ value:"", label:"默认", tagType:"success" },
								{ value:0, label:"默认", tagType:"success" },
								{ value:1, label:"登录", tagType:"danger" },
								{ value:2, label:"登录及本地", tagType:"danger" },
							]
						},
						{ key:"sort", title:"序列", type:"number"},
						{ key:"_add_time", title:"创建时间", type:"time", sortable:"custom"},
					],
					// 多选框选中的值
					multipleSelection:[],
					// 当前高亮的记录
					selectItem:"",
					// rightBtnsMore:[
					// 	{
					// 		title:'按钮1',
					// 		onClick:function(item){
					// 			vk.toast(`${item._id}-按钮1`);
					// 		}
					// 	},
					// 	{
					// 		title:'按钮2',
					// 		onClick:function(item){
					// 			vk.toast(`${item._id}-按钮2`);
					// 		}
					// 	}
					// ]
				}
			}
		},
		onPageScroll(e) {
		},
		// 监听 - 页面每次【加载时】执行(如：前进)
		onLoad(options = {}) {
			that = this;
			vk = that.vk;
			that.options = options;
			that.init(options);
		},
		// 监听 - 页面【首次渲染完成时】执行。注意如果渲染速度快，会在页面进入动画完成前触发
		onReady(){
			
		},
		// 监听 - 页面每次【显示时】执行(如：前进和返回) (页面每次出现在屏幕上都触发，包括从下级页面点返回露出当前页面)
		onShow() {

		},
		// 监听 - 页面每次【隐藏时】执行(如：返回)
		onHide() {
			
		},
		// 监听 - 页面触底部
		onReachBottom(){
			
		},
		// 监听 - 窗口尺寸变化(仅限:App、微信小程序)
		onResize(){
			
		},
		// 监听 - 点击右上角转发时
		onShareAppMessage(options) {
			
		},
		// 监听 - 页面创建时
		created() {
			
		},
		// 函数
		methods: {
			// 页面数据初始化函数
			init(options) {
				originalForms["form"] = vk.pubfn.copyObject(that.form);
			},
			// 表单重置
			resetForm(){
				vk.pubfn.resetForm(originalForms, that);
			},
			// 显示添加页面
			addBtn(){
				that.resetForm();
				that.form.props.action = 'admin/category/sys/add';
				that.form.props.formType = 'add';
				that.form.props.title = '添加';
				that.form.props.show = true;
				let currentRow = that.getCurrentRow();
				if(currentRow && currentRow.category_id){
					that.$set(that.form.data,"parent_id", currentRow.category_id);
				}
			},
			// 搜索
			search(){
				that.$refs.table.search();
			},
			// 刷新
			refresh(){
				that.$refs.table.refresh();
			},
			// 监听 - 行的选中高亮事件
			currentChange(val){
				that.table.selectItem = val;
			},
			// 获取当前选中的行的数据
			getCurrentRow(){
				let currentRow = that.$refs.table.getCurrentRow();
				console.log(currentRow);
				return currentRow;
			},
			// 显示修改页面
			updateBtn({ item }){
				that.form.props.action = 'admin/category/sys/update';
				that.form.props.formType = 'update';
				that.form.props.title = '编辑';
				that.form.props.show = true;
				that.form.data = item;
			},
			// 删除按钮
			deleteBtn({ item, deleteFn }){
				deleteFn({
					action:"admin/category/sys/delete",
					data:{
						_id: item._id
					},
				});
			},
		},
		// 过滤器
		filters:{
			
		},
		// 计算属性
		computed:{
			
		}
	}
</script>
<style lang="scss" scoped>
	
	
</style>
